-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fully qualify type references; remove usings #60
Fully qualify type references; remove usings #60
Conversation
public partial interface IDemoClass | ||
{ | ||
/// <inheritdoc /> | ||
string CMethod<T, T1, T2, T3, T4>(string x, string y) where T : class where T1 : struct where T3 : DemoClass where T4 : IDemoClass; | ||
string CMethod<T, T1, T2, T3, T4>(string x, string y) where T : class where T1 : struct where T3 : global::AutomaticInterfaceExample.DemoClass where T4 : IDemoClass; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the one place where types aren't fully qualified (where T4: IDemoClass
).
IDemoClass
doesn't exist at the time of generation, so it can't be resolved. This is OK though, since the reference is within the scope of IDemoClass
, so no collision is possible
@@ -18,6 +18,15 @@ public static class Builder | |||
| SymbolDisplayParameterOptions.IncludeParamsRefOut | |||
); | |||
|
|||
private static readonly SymbolDisplayFormat TypeDisplayFormat = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New display format for types. Based on SymbolDisplayFormat.FullyQualifiedFormat
, but with support for nullables, and without unnecessary non-type parameters
cb.AppendLine($"namespace {nameSpaceName}"); | ||
cb.AppendLine("{"); | ||
|
||
cb.Indent(); | ||
|
||
cb.AppendAndNormalizeMultipleLines(classDocumentation); | ||
|
||
cb.AppendLine($"[GeneratedCode(\"AutomaticInterface\", \"\")]"); | ||
cb.AppendLine( | ||
"[global::System.CodeDom.Compiler.GeneratedCode(\"AutomaticInterface\", \"\")]" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was a choice - it seemed simpler and more concise to fully qualify this too, since otherwise there's only be one using in the interface file, using System.CodeDom.Compiler;
If you don't like this choice, just say and I'll change it back :)
a4af269
to
f2df1bc
Compare
@@ -257,61 +245,12 @@ public partial interface IDemoClass | |||
GenerateCode(code).Should().Be(expected); | |||
} | |||
|
|||
[Fact] | |||
public void AddsUsingsToInterface() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Obsolete test
} | ||
|
||
[Fact] | ||
public void WorksWithFileScopedNamespace() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved into GeneratorTests.TypeResolution
9dc889a
to
c90392a
Compare
This updates the interface generation to always fully qualify type references, and no longer generate "using" statements in generated interfaces. This makes the interface generation resilient to namespace collisions and other namespace edge-cases (like resolving global usings in parent assemblies). It also means the generator is now able to correctly resolve types with duplicate names
c90392a
to
296d31c
Compare
Without `global::` types in interfaces, the generated interface will resolve to the locally-defined type instead of the global one
The line above already asserts that there should be no errors without using `MissingUsingsAreOk`, so the extra check does nothing
sourceDiagnostics | ||
.Where(d => d.Severity == DiagnosticSeverity.Error) | ||
.Where(x => x.Id != "CS0246") | ||
.Where(x => !MissingUsingsAreOk(x)) | ||
.Should() | ||
.BeEmpty(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check was already covered in the broader check on lines 31-35:
net_automatic_interface/AutomaticInterface/Tests/GeneratorTests.cs
Lines 31 to 35 in e151b05
sourceDiagnostics | |
.Where(d => d.Severity == DiagnosticSeverity.Error) | |
.Where(x => x.Id != "CS0246") // missing references are ok | |
.Should() | |
.BeEmpty(); |
Looks very good, thank you so much! |
This updates the interface generation to always fully qualify type references, and no longer generate "using" statements in generated interfaces.
This makes the interface generation resilient to namespace collisions and other namespace edge-cases (like resolving global usings in parent assemblies). It also means the generator is now able to correctly resolve types with duplicate names
Addresses #55